--- %%NOBANNER%% -->
/*-------------------<-- Start of Description-->---------------------\
| BNMLCI: Exact Confidence Interval for proportions; |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|-----------<-- Start of Files or Arguements Needed-->---------------|
| Arguments: |
| WIDTH =95, for a 95% CI, 99 for a 99% CI etc; |
| Default is 95; |
| X =Observed number of successes in N trials; |
| N =Number of binomial trials; |
| Optional Parameters: Used to generate a TABLE of exact CIs for all |
| values of X for each N ranging from NMIN to NMAX. |
| NMIN = smallest value of N to table; |
| NMAX = largest value of N to table; |
| Note that the parameters X and N are ignored if one is creating |
| a table. |
| Methodology: The macro is based on the exact relationship between |
| the beta dist'n and the cumulative binomial as described |
| in Feller(see above). |
| Output: The results of a PRINT on the data set _exact_. |
| Upper & lower confidence limits for the 3 methods. |
|------------<-- End of Files or Arguements Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: |
| %BNMLCI(x=6,n=93); |
| *Table of exact 95pct CIs for all possible Xs for N=10 to 15; |
| %bnmlci(nmin=10,nmax=15); |
| Usage: %BNMLCI(width=,x=,n=,nmin=,nmax=); |
\------------------<-- Start of Files Created-->--------------------*/
%MACRO bnmlci(WIDTH=95,x=,n=,nmin=,nmax=);
/*--------------------------------------------\
| Author: Duo Zhou; |
| Created: 2-1-2002 7:21pm; |
| Purpose: Binomial Exact Confidence Interval;|
\--------------------------------------------*/
Data _exact_;
IF ^(10 LE &WIDTH LE 99) THEN DO;
PUT "NOT EXECUTED: CONFIDENCE INTERVAL WIDTH IS LT 10 OR GT 99";
STOP;
END;
%if &nmin= %then %do;
IF &X GT &N OR &X LT 0 OR &N LT 2 THEN DO;
PUT "NOT EXECUTED: X IS LT 0 OR X GT N OR N LT 2";
STOP;
END;
%end;
LPCT=(1-&WIDTH/100)/2;
UPCT=1-LPCT;
%if &x^= %then %do; x=&x; %end;
%if &n^= %then %do; n=&n; %end;
%if &nmin^= %then %do;
do n=&nmin to &nmax;
do x=0 to n;
%end;
Phat=x/n;
**NORMAL APPROXIMATION;
L_normal=PHAT+PROBIT(LPCT)*SQRT(PHAT*(1-PHAT)/N); *LOWER LIMIT;
U_normal=PHAT+PROBIT(UPCT)*SQRT(PHAT*(1-PHAT)/N); *UPPER LIMIT;
**POISSON APPROXIMATION;
DF_LO=2*X;
DF_UP=2*(X+1);
IF X GT 0 THEN L_poissn=GAMINV(LPCT,DF_LO/2)/N; *LOWER LIMIT;
ELSE L_POI=0;
U_poissn=GAMINV(UPCT,DF_UP/2)/N; *UPPER LIMIT;
*** EXACT BINOMIAL CONFIDENCE LIMITS ***equn 10.7 of Feller*;
* Upper limit;
if x0 then l_exact=1-betainv(upct,n-x+1,x);
else l_exact=0;
output;
%if &nmin^= %then %do;
end;
end;
%end;
FORMAT PHAT L_normal U_normal L_poissn U_poissn L_exact U_exact 6.4;
label phat='Phat*(x/N)' l_exact='Exact CI*Lower' u_exact='Exact CI*Upper'
l_normal='Normal*Approx.*Lower' u_normal='Normal*Approx.*Upper'
l_poissn='Poisson*Approx.*Lower' u_poissn='Poisson*Approx.*Upper';
proc print data=_exact_ split='*'; by n; id n;
var x phat l_exact u_exact l_normal u_normal l_poissn u_poissn;
title5"&width Percent Confidence Int. for Binomial(P),N trials,x successes)";
title6"Exact intervals are based on Feller(Wiley 1968, eqn 10.7). ";
title7"Also printed are Normal & Poisson approximations to the binomial."; run;
title5; run;
%mend bnmlci;